package com.senlingspace.web.controller.api;

import com.senlingspace.common.core.controller.BaseController;
import com.senlingspace.common.core.domain.AjaxResult;
import com.senlingspace.common.utils.StringUtils;
import com.senlingspace.payment.douyin.bean.request.DouyinProductAuditRequest;
import com.senlingspace.payment.douyin.service.ISlkjDouyinRecallService;
import com.senlingspace.system.domain.dto.DeviceVisitDto;
import com.senlingspace.system.domain.dto.TeamStatusDto;
import com.senlingspace.system.domain.vo.AppInfoVo;
import com.senlingspace.system.service.ISlkTeamStatusService;
import com.senlingspace.system.service.ISlkjTeamEqumentService;
import com.senlingspace.system.service.ISlkjVersionUpdateService;
import com.senlingspace.system.service.SlkjOrderTourDoneService;
import com.senlingspace.web.controller.douyin.DouYinOrderController;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 此controller映射路径无需身份校验
 * 用户游览完成后接口调用，修改订单状态
 *
 * @author wangliang
 */
@RestController
@RequestMapping("/api")
@Slf4j
public class SlkjOrderTourDoneController extends BaseController {

    @Autowired
    private SlkjOrderTourDoneService slkjOrderTourDoneService;

    @Autowired
    private ISlkjTeamEqumentService slkjTeamEqumentService;

    @Autowired
    private ISlkjVersionUpdateService slkjVersionUpdateService;

    @Autowired
    private ISlkjDouyinRecallService slkjDouyinRecallService;
    @Autowired
    private DouYinOrderController douYinOrderController;

    /**
     *
     * 获取app版本号和下载地址
     * @author wangliang
     * @param appType
     * @return com.senlingspace.system.domain.vo.AppInfoVo
     */
    @GetMapping(value = "/app/version")
    public AjaxResult list(@RequestParam String appType) {
        AppInfoVo appInfoVo = slkjVersionUpdateService.selectVersionVo(appType);
        return AjaxResult.success(appInfoVo);
    }


    /**
     * 抖音消息回调接口
     * */
    @PostMapping("/douyin/callbackUrl")
    public Object updateOrderBusinessStatus(@RequestBody DouyinProductAuditRequest douyinProductAuditRequest,
                                           HttpServletRequest request) {
        String msgId = request.getHeader("Msg-Id");
        log.info("抖音-回调方法被调用 msgId"+msgId);
        log.info("请求参数：" + douyinProductAuditRequest.toString());

        if (StringUtil.isBlank(msgId)) {
            return error(" 抖音消息回调接口 请求头Msg_Id为空");
        }
        douyinProductAuditRequest.setMsgId(msgId);
        int res = slkjDouyinRecallService.insertSlkjDouyinRecall(douyinProductAuditRequest);
        return douyinProductAuditRequest.getContent();
    }


    /**
     * 获取组队设备领取信息
     */
    @GetMapping(value = "/listByIsEqument")
    public AjaxResult listByIsEqument(Long shopId) {
        HashMap<String, List> resIsEqument = slkjTeamEqumentService.listByIsEqument(shopId);
        return AjaxResult.success(resIsEqument);
    }

    /**
     * 修改设备组队关联设备领取状态
     */
    @PostMapping("/updateByIsEqument")
    public AjaxResult edit(@RequestBody HashMap<String, String> equmentRequest) {
// todo 查询一下订单状态如果是退款的 不展示

        String ids = equmentRequest.get("ids");
        String isEqumentStr = equmentRequest.get("isEqument");

        if (StringUtils.isBlank(ids) || StringUtils.isBlank(isEqumentStr)) {
            return error("参数缺失");
        }

        Integer isEqument = Integer.parseInt(isEqumentStr);

        long[] idArray = Arrays.stream(ids.split(",")).mapToLong(Long::parseLong).toArray();

        int updateNum = slkjTeamEqumentService.updateByIsEqument(idArray, isEqument);

        return toAjax(updateNum);
    }


    @PostMapping("/device/done")
    public AjaxResult updateOrderBusinessStatus(@RequestBody HashMap<String, String> deviceVisitDto) {

        log.error("/device/done 接口请求参数如下：");
        for (String s : deviceVisitDto.keySet()) {
            log.error(s + "-> " + deviceVisitDto.get(s));
        }

        DeviceVisitDto deviceVisitDto1 = new DeviceVisitDto();
        deviceVisitDto1.setOrderNumber(deviceVisitDto.get("orderNumber"));
        deviceVisitDto1.setUuid(deviceVisitDto.get("uuid"));
        deviceVisitDto1.setGroupId(Long.parseLong(deviceVisitDto.get("groupId")));
        deviceVisitDto1.setPlayerName(deviceVisitDto.get("playerName"));


        slkjOrderTourDoneService.updateOrderBusinessStatus(deviceVisitDto1);

        return success();
    }

}
